import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//1.引入element-plus组件库
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
//3.引入element-plus图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//5.1修改elementPlus默认中文
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";

//8.1配置根路径
const BASE_URL="http://localhost:8080";
//给内置全局对象window设置全局内容，可以在任意script标签内部使用
window.BASE_URL=BASE_URL;

const app = createApp(App)
//8.2给app全局对象配置根路径，可以在template标签内部使用BASE_URL
app.config.globalProperties.BASE_URL=BASE_URL;

//4.遍历ElementPlusIconsVue对象中的所有图标组件并注册到vue实例app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//2.use（ElementPlus）给当前vue实例使用组件库
//5.2 app.use(ElementPlus,{ locale: zhCn })将El本地化为中文
app.use(store).use(router).use(ElementPlus,{ locale: zhCn }).mount('#app')

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}

//7.定义获取当前登录人数据的全局方法
window.getUser=()=>{
  return localStorage.user?JSON.parse(localStorage.user) : null
}

//9.创建加载字典项的全局方法，加载某个字典下管理的所有字典项
window.loadDictOptions=(object,dictCode)=>{
    axios.get(BASE_URL+"/v1/dictOption/selectOptions?code="+dictCode)
        .then((response)=>{
            if(response.data.code==2000){
                object.value=response.data.data;
                ElMessage.success("字典项查询成功");
            }else{
                ElMessage.error("字典项查询失败");
            }
        })
}
